package com.easyban.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easyban.dto.UserDto;
import com.easyban.entity.User;
import com.easyban.entity.UserAddress;
import com.easyban.mapper.UserAddressMapper;
import com.easyban.mapper.UserMapper;
import com.easyban.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 新增用户及添加地址
     *
     * @param userDto
     */
    @Override
    @Transactional
    public void saveUserDto(UserDto userDto) {
        User user = new User();
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(userDto, user);
        BeanUtils.copyProperties(userDto, userAddress);
        userAddress.setIsDefault(1);
        userMapper.insert(user);
        userAddressMapper.insert(userAddress);
    }

    /**
     * 查找用户及添加地址
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public UserDto getOnes(Long id) {
        User user = userMapper.selectById(id);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto);

        //查找地址
        LambdaQueryWrapper<UserAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAddress::getUserId, userDto.getUserId());
        queryWrapper.eq(UserAddress::getIsDefault, 1);
        UserAddress userAddress = userAddressMapper.selectOne(queryWrapper);
        //存入地址和默认值
        userDto.setAddress(userAddress.getAddress());
        userDto.setIsDefault(userAddress.getIsDefault());

        return userDto;
    }

    /**
     * @param userDto
     */
    @Transactional
    @Override
    public void updateUser(UserDto userDto) {
        User user = new User();
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(userDto, user);
        BeanUtils.copyProperties(userDto, userAddress);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, user.getId());
        userMapper.update(user, queryWrapper);

        LambdaQueryWrapper<UserAddress> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(UserAddress::getUserId, userAddress.getUserId());
        queryWrapper1.eq(UserAddress::getIsDefault, 1);
        userAddressMapper.update(userAddress, queryWrapper1);
    }

    /**
     * @param id
     */
    @Transactional
    @Override
    public void removeUser(Long id) {
        User user = getById(id);


        LambdaQueryWrapper<UserAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAddress::getUserId, user.getUserId());
        userMapper.deleteById(id);
        userAddressMapper.delete(queryWrapper);
    }
}
